package com.atguigu.srb.core.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.common.result.R;
import com.atguigu.srb.core.listener.ExcelDictDTOListener;
import com.atguigu.srb.core.pojo.dto.ExcelDictDTO;
import com.atguigu.srb.core.pojo.entity.Dict;
import com.atguigu.srb.core.mapper.DictMapper;
import com.atguigu.srb.core.service.DictService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 数据字典 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-09-13
 */
@Service
@Slf4j
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {

    @Resource
    private DictService dictService;
    @Override
    public void batchImport(InputStream inputStream) {
        EasyExcel.read(inputStream, ExcelDictDTO.class,new ExcelDictDTOListener(baseMapper)).sheet("数据字典").doRead();
        log.info("batchImport finished");
    }

    @Override
    public List<ExcelDictDTO> excelDictDTOS() {
        List<ExcelDictDTO> excelDictDTOS = new ArrayList<>();
        List<Dict> dicts = baseMapper.selectList(null);
       excelDictDTOS = dicts.stream().map((dict -> {
           ExcelDictDTO excelDictDTO = new ExcelDictDTO();
           BeanUtils.copyProperties(dict, excelDictDTO);
           return excelDictDTO;
       })).collect(Collectors.toList());
        return excelDictDTOS;
    }

    @Override
    public List<Dict> getDictListByParentId(Long parentId) {
        QueryWrapper<Dict> dictQueryWrapper = new QueryWrapper<>();
        dictQueryWrapper.eq("parent_id", parentId);
        List<Dict> dicts = baseMapper.selectList(dictQueryWrapper);
        for (Dict dict : dicts) {
            dict.setHasChildren(this.hasChildren(dict.getId()));
        }
        return dicts;
    }

    @Override
    public List<Dict> getDictListByDictCode(String dictCode) {
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dict_code",dictCode);
        Dict dict = baseMapper.selectOne(queryWrapper);
        Long parentId = dict.getId();
        queryWrapper.clear();
        queryWrapper.eq("parent_id",parentId);
        List<Dict> dictlist = baseMapper.selectList(queryWrapper);
        return dictlist;
    }

    @Override
    public String getDictNameByDictCodeAndValueId(String dictCode, Integer valueId) {
        QueryWrapper<Dict> dictQueryWrapper = new QueryWrapper<>();
        dictQueryWrapper.eq("dict_code",dictCode);
        Dict dict = baseMapper.selectOne(dictQueryWrapper);
        Long parentId = dict.getId();
        dictQueryWrapper.clear();
        dictQueryWrapper.eq("parent_id",parentId)
                .eq("value",valueId);
        Dict dictResult = baseMapper.selectOne(dictQueryWrapper);
        String name = dictResult.getName();
        return name;
    }

    public boolean hasChildren(Long id){
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<Dict>();
        queryWrapper.eq("parent_id", id);
        Integer integer = baseMapper.selectCount(queryWrapper);
        if (integer <= 0){
            return false;
        }else return true;

    }

}
